package cc.io24.knewcafe.boss.controller;

import cc.io24.knewcafe.boss.dao.SysUserDao;
import cc.io24.knewcafe.boss.domain.SysUser;
import cc.io24.knewcafe.boss.service.JobService;
import cc.io24.knewcafe.boss.vo.JobDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/job")
public class JobDetailController {

    @Autowired
    JobService jobDetailService;
    @Autowired
    SysUserDao sysUserDao;

    @RequestMapping(value = "",produces="application/json")
    public @ResponseBody
    JobDetail hello(){
        JobDetail job = new JobDetail();
        job.setCrawlerType("123");
        job.setRetryNumber(0);
        job.setExecuteTime(new Date());
        jobDetailService.startJob(job);
        return job;
    }

    @RequestMapping(value = "h",produces="application/json")
    public @ResponseBody
    List<SysUser>  h(){
        List<SysUser> users = sysUserDao.findAll();
        for(SysUser sysUser:users){
            System.out.println(sysUser.getRoles().size());

        }
        return users;
    }

    @RequestMapping(value = "test",produces="application/json")
    public @ResponseBody
    List<JobDetail> hello(@ModelAttribute JobDetail job,
                          @PageableDefault(value = 10, sort = { "id" }, direction = Sort.Direction.DESC)Pageable pageable,
                          HttpServletResponse response){
        response.addHeader("X-Total-Count","112");
        response.addHeader("Access-Control-Expose-Headers","X-Total-Count");

        return jobDetailService.findList(job, pageable);
    }

    @PostMapping(value="/start",produces="application/json")
    public @ResponseBody
    boolean start(@RequestBody JobDetail jobDetail){
        boolean ret = jobDetailService.startJob(jobDetail);
        return ret;
    }



}
